package com.itholmes.utils;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.JedisCluster;

public class JedisClusterUtils {

	//创建GenericObjectPoolConfig对象配置
	static GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
	//获得genericObjectPool对象，连接池对象
	static GenericObjectPool<JedisCluster> objectPool = new GenericObjectPool<JedisCluster>(new JedisClusterPoolFactory(),genericObjectPoolConfig);

	static {
		//最大闲置数
		genericObjectPoolConfig.setMaxIdle(10);
		//最小闲置数
		genericObjectPoolConfig.setMinIdle(5);
		//最大连接数
		genericObjectPoolConfig.setMaxTotal(100);
	}
	
	public static JedisCluster getJedisCluster() {
		//JedisCluster对象使用该对象来连接redis集群
		JedisCluster cluster = null;
		try {
			//通过连接池生成jedisCluster对象
			cluster = objectPool.borrowObject();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cluster;
	}
	
	/*
	 * String类型方法封装：
	 */
	public static Long setnx_Clus(String key,String value) {
		JedisCluster cluster = getJedisCluster();
		Long setnx = cluster.setnx(key, value);
		cluster.close();
		return setnx;
	}
	
	public static String get_Clus(String key) {
		JedisCluster cluster = getJedisCluster();
		String string = cluster.get(key);
		cluster.close();
		return string;
	}
	
	public static String setex_Clus(String key,String value,int seconds) {
		JedisCluster cluster = getJedisCluster();
		String setex = cluster.setex(key, seconds, value);
		cluster.close();
		return setex;
	}
	
	public static Long ttl_Clus(String key) {
		JedisCluster cluster = getJedisCluster();
		Long ttl = cluster.ttl(key);
		cluster.close();
		return ttl;
	}
	
	public static Long incrBy_Clus(String key,Long increment) {
		JedisCluster cluster = getJedisCluster();
		Long by = cluster.incrBy(key, increment);
		cluster.close();
		return by;
	}
	
	//因为是集群的缘故，必须要用组的方式来传值
	public static String mset_Clus(Map<String, String>  map) {
		String keysvalues = "";
		Set set = map.entrySet();
		Iterator iterator = set.iterator();
		while(iterator.hasNext()) {
			Object next = iterator.next();
			
			Map.Entry entry = (Map.Entry)next;
			keysvalues += entry.getKey()+","+entry.getValue()+",";
		}
		keysvalues = keysvalues.substring(0, keysvalues.length()-1);
		JedisCluster cluster = getJedisCluster();
		String mset = cluster.mset(keysvalues);
		cluster.close();
		return mset;
	}
	
	public static List<String> mget_Clus(List<String> list) {
		String keys = "";
		for(String l:list) {
			keys+=l+",";
		}
		keys = keys.substring(0, keys.length()-1);
		JedisCluster cluster = getJedisCluster();
		List<String> list2 = cluster.mget(keys);
		cluster.close();
		return list2;
	}
	
	
//	Map<String,String> map = new HashMap<String, String>();
//	map.put("mset{user2}", "mvalue");
//	map.put("mset2{user2}", "mvalue2");
//	map.put("mset3{user2}", "mvalue3");
//	String mset_Clus = mset_Clus(map);
//	
//	List<String> list = new ArrayList<String>();
//	list.add("mset{user2}");
//	list.add("mset2{user2}");
//	list.add("mset3{user2}");
//	List<String> mget_Clus = mget_Clus(list);
	
	
	/*
	 * Hash类型方法封装：
	 */
	
	public static String hget_Clus(String key,String field) {
		JedisCluster cluster = getJedisCluster();
		String hget = cluster.hget(key, field);
		cluster.close();
		return hget;
	}
	
	public static Long hset_Clus(String key,String field,String value) {
		JedisCluster cluster = getJedisCluster();
		Long hset = cluster.hset(key,field,value);
		cluster.close();
		return hset;
	}
	
	public static Long hdel_Clus(String key,String field) {
		JedisCluster cluster = getJedisCluster();
		Long hdel = cluster.hdel(key, field);
		cluster.close();
		return hdel;
	}
	
	public static Long hincrby_Clus(String key , String field , Long value) {
		JedisCluster cluster = getJedisCluster();
		Long hincrBy = cluster.hincrBy(key, field, value);
		cluster.close();
		return hincrBy;
	}
	
	public static Set<String> hkeys_Clus(String key) {
		JedisCluster cluster = getJedisCluster();
		Set<String> set = cluster.hkeys(key);
		cluster.close();
		return set;
	}
	
	public static List<String> hvals_Clus(String key) {
		JedisCluster cluster = getJedisCluster();
		List<String> list = cluster.hvals(key);
		cluster.close();
		return list;
	}
	
	/*
	 * List类型方法封装：
	 */
	public static Long lpush_Clus(String key,String string) {
		JedisCluster cluster = getJedisCluster();
		Long lpush = cluster.lpush(key, string);
		cluster.close();
		return lpush;
	}
	public static String lindex_Clus(String key,int index) {
		JedisCluster cluster = getJedisCluster();
		String lindex = cluster.lindex(key, index);
		cluster.close();
		return lindex;
	}
	public static String lpop_Clus(String key) {
		JedisCluster cluster = getJedisCluster();
		String lpop = cluster.lpop(key);
		cluster.close();
		return lpop;
	}
	public static String lset_Clus(String key,int index,String value) {
		JedisCluster cluster = getJedisCluster();
		String lset = cluster.lset(key, index, value);
		cluster.close();
		return lset;
	}
	
	
	/*
	 * Set类型方法封装：
	 */
	public static Long sadd_Clus(String key,String member) {
		JedisCluster cluster = getJedisCluster();
		Long sadd = cluster.sadd(key, member);
		cluster.close();
		return sadd;
	}
	
	public static Set<String> smembers_Clus(String key) {
		JedisCluster cluster = getJedisCluster();
		Set<String> set = cluster.smembers(key);
		cluster.close();
		return set;
	}
	public static Boolean sismember_Clus(String key,String member) {
		JedisCluster cluster = getJedisCluster();
		Boolean sismember = cluster.sismember(key, member);
		cluster.close();
		return sismember;
	}
	public static Long srem_Clus(String key,String member) {
		JedisCluster cluster = getJedisCluster();
		Long srem = cluster.srem(key, member);
		cluster.close();
		return srem;
	}
	
	/*
	 * sorted set类型方法封装：
	 */
	public static Long zadd_Cluster(String key,double score,String member) {
		JedisCluster cluster = getJedisCluster();
		Long zadd = cluster.zadd(key, score,member);
		cluster.close();
		return zadd;
	}
	
	public static Set<String> zrange_Clus(String key,int start,int stop) {
		JedisCluster cluster = getJedisCluster();
		Set<String> set = cluster.zrange(key, start, stop);
		cluster.close();
		return set;
	}
	
	public static Double zincrby_Clus(String key,int increment,String member) {
		JedisCluster cluster = getJedisCluster();
		Double zincrby = cluster.zincrby(key, increment, member);
		cluster.close();
		return zincrby;
	}
	
	public static Long zrem_Clus(String key,String members) {
		JedisCluster cluster = getJedisCluster();
		Long zrem = cluster.zrem(key, members);
		cluster.close();
		return zrem;
	}
	
	public static Double zscore_Clus(String key,String member) {
		JedisCluster cluster = getJedisCluster();
		Double zscore = cluster.zscore(key, member);
		cluster.close();
		return zscore;
	}
	
	public static Long zcount_Clus(String key,Long min,Long max) {
		JedisCluster cluster = getJedisCluster();
		Long zcount = cluster.zcount(key, min, max);
		cluster.close();
		return zcount;
	}
	
	public static Long zcard_Clus(String key) {
		JedisCluster cluster = getJedisCluster();
		Long zcard = cluster.zcard(key);
		cluster.close();
		return zcard;
	}

	public static void main(String[] args) {
		
		
		
	}
}
